/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * addAcc.java
 *
 * Created on 03/05/2010, 03:10:04 PM
 */

package UI;
import Excepciones.AccesorioException;
import Excepciones.BasesdeDatos;
import Excepciones.DinamicaDato;
import Excepciones.ProcesamientoDato;
import Excepciones.Sistema;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import robot_maker.*;

/**
 *
 * @author Miguel
 */
public class addAccesorio extends javax.swing.JFrame {

    /** Creates new form addAcc */
    public addAccesorio() {
        initComponents();
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);
    }
    private UI_Accesorios owner;
    private boolean modify;
    private int idRobot;
    private String rName;
    addAccesorio(UI_Accesorios aThis, boolean mod, int id) {
          initComponents();
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);
        modify = mod;
        owner = aThis;
        idRobot = id;

        if(mod){
            Accesorio datos = new Accesorio();
            try {
                try {
                    datos = datos.getDatos(id);
                } catch (ProcesamientoDato ex) {
                }
            } catch (Exception ex) {
         Sistema.loggear(11, "Contructor",
                        "EN EL SQL DE EJECUCION",
                        addAccesorio.class, "SQLException");
            }
            nombre_acc.setText(datos.getNombre());
            rName = datos.getNombre();
            desc_acc.setText(datos.getCaracteristicas());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        nombre_acc = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        bot_guardarAccesorio = new javax.swing.JButton();
        bot_CancelarAccesorio = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        desc_acc = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Agregar Accesorio:");

        jLabel2.setText("Nombre:");

        jLabel3.setText("Descripción:");

        bot_guardarAccesorio.setText("Guardar");
        bot_guardarAccesorio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bot_guardarAccesorioActionPerformed(evt);
            }
        });

        bot_CancelarAccesorio.setText("Cancelar");
        bot_CancelarAccesorio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bot_CancelarAccesorioActionPerformed(evt);
            }
        });

        desc_acc.setColumns(20);
        desc_acc.setRows(5);
        jScrollPane1.setViewportView(desc_acc);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(261, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(bot_CancelarAccesorio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(bot_guardarAccesorio))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(nombre_acc)
                            .addComponent(jScrollPane1))))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nombre_acc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bot_guardarAccesorio)
                    .addComponent(bot_CancelarAccesorio))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bot_guardarAccesorioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bot_guardarAccesorioActionPerformed
   try {
            Accesorio acc = null;
            acc = new Accesorio(nombre_acc.getText(), desc_acc.getText());
            if (modify) {
                acc.setId(idRobot);
                acc.modificarBD(acc, rName);
            } else {
                try {
                    acc.ingresarBD(acc);
                } catch (Exception ex) {

            Sistema.loggear(11, "Guardar",
                        "EN EL SQL DE EJECUCION",
                        addAccesorio.class, "SQLException");

        }
            }
            this.setVisible(false);

        } catch (ProcesamientoDato ex) {
            Logger.getLogger(addAccesorio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AccesorioException ex) {
        }
        try {
            owner.refrescar();
        } catch (Exception ex) {
           Sistema.loggear(11, "Guardar",
                        "EN EL SQL DE EJECUCION",
                        addAccesorio.class, "SQLException");
        }
            

}//GEN-LAST:event_bot_guardarAccesorioActionPerformed

    private void bot_CancelarAccesorioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bot_CancelarAccesorioActionPerformed
        try {
            owner.refrescar();
        } catch (SQLException ex) {
            Sistema.loggear(11, "Cancelar",
                        "EN EL SQL DE EJECUCION",
                        addAccesorio.class, "SQLException");
        }

    }//GEN-LAST:event_bot_CancelarAccesorioActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new addAccesorio().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bot_CancelarAccesorio;
    private javax.swing.JButton bot_guardarAccesorio;
    private javax.swing.JTextArea desc_acc;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField nombre_acc;
    // End of variables declaration//GEN-END:variables

}
